package com.gdth.bank.model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import rx.Observable;
import rx.Subscriber;

import com.gdth.bank.BankApplication;
import com.gdth.bank.bean.SynchronizeBean;
import com.gdth.database.bean.DaoSession;
import com.gdth.database.bean.DzCpkhXxDjEtc;
import com.gdth.database.bean.DzCpkhXxDjEtcDao;
import com.gdth.database.bean.TAppFjxxb;
import com.gdth.database.bean.TAppFjxxbDao;
import com.gdth.http.method.HttpMethodServlet;
import com.gdth.utils.GsonUtil;
import com.gdth.utils.StringUtils;

import org.greenrobot.greendao.query.DeleteQuery;
import org.greenrobot.greendao.query.QueryBuilder;


public class DzCpKhXxEtcModel {
	/*public Observable<List<DzCpkhXxDjEtc>> saveDzcpkhxxetc(final DzCpkhXxDjEtc mDzCpkhXxDjEtc){
		return Observable.create(new Observable.OnSubscribe<List<DzCpkhXxDjEtc>>() {

			@Override
			public void call(Subscriber<? super List<DzCpkhXxDjEtc>> arg0) {
				// TODO Auto-generated method stub
				DzCpkhXxDjEtcDao dao = BankApplication.getDaoSession().getDzCpkhXxDjEtcDao();
				dao.insert(mDzCpkhXxDjEtc);
			Long id=mDzCpkhXxDjEtc.getId();
			if (id != null){
			   dao.update(mDzCpkhXxDjEtc);
			}else{
				dao.insert(mDzCpkhXxDjEtc);
			}
			List<DzCpkhXxDjEtc> mList = new ArrayList<DzCpkhXxDjEtc>();
			mList.add(mDzCpkhXxDjEtc);
			if(mList==null){
				arg0.onError(null);
			}else{
				arg0.onNext(mList);
				arg0.onCompleted();
			}
			
			}
		});
	}*/
	
	public Observable<List<TAppFjxxb>> getFjXx(final String uuid){
		return Observable.create(new Observable.OnSubscribe<List<TAppFjxxb>>() {

			@Override
			public void call(Subscriber<? super List<TAppFjxxb>> arg0) {
				// TODO Auto-generated method stub
				TAppFjxxbDao fjDao= BankApplication.getDaoSession().getTAppFjxxbDao();
				QueryBuilder<TAppFjxxb> fjqb=fjDao.queryBuilder();
				String wybs = uuid;
				fjqb.where(TAppFjxxbDao.Properties.Wybs.eq(uuid));
				List<TAppFjxxb> fjListc = fjqb.list();
				fjqb.where(TAppFjxxbDao.Properties.Sczt.eq(0));
				List<TAppFjxxb> fjList = fjqb.list();
				if (fjList == null){
					arg0.onError(null);
				} else {
					
					arg0.onNext(fjList);
					arg0.onCompleted();
				}
				
				
			}	
		});
	}
	
	public Observable<List<DzCpkhXxDjEtc>> saveDzcpkhxxetc(final DzCpkhXxDjEtc mDzCpkhXxDjEtc){
		return Observable.create(new Observable.OnSubscribe<List<DzCpkhXxDjEtc>>() {

			@Override
			public void call(final Subscriber<? super List<DzCpkhXxDjEtc>> arg0) {
				// TODO Auto-generated method stub
				final DaoSession session = BankApplication.getDaoSession();
				session.runInTx(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						
						boolean re = true;
						Exception ex = null;
						
						DzCpkhXxDjEtcDao dao = session.getDzCpkhXxDjEtcDao();
						String wybs = mDzCpkhXxDjEtc.getAppUuid();
						try{
							DzCpkhXxDjEtc aDzCpkhXxDjEtc=mDzCpkhXxDjEtc;
							dao.insertOrReplace(mDzCpkhXxDjEtc);
							Long id = mDzCpkhXxDjEtc.getId();
							//System.out.println(id);
							if (mDzCpkhXxDjEtc.getFjxxs() != null && mDzCpkhXxDjEtc.getFjxxs().size() > 0) {
								TAppFjxxbDao fjxxDao = session.getTAppFjxxbDao();
								QueryBuilder<TAppFjxxb> fjxxqb = fjxxDao.queryBuilder();
								fjxxqb.where(TAppFjxxbDao.Properties.Wybs.eq(wybs));
							  DeleteQuery<TAppFjxxb> deletefjxx = fjxxqb.buildDelete();
								deletefjxx.executeDeleteWithoutDetachingEntities();
								fjxxDao.insertOrReplaceInTx(mDzCpkhXxDjEtc.getFjxxs());
							}
							/*TAppFjxxbDao fjxxDao = session.getTAppFjxxbDao();
							QueryBuilder<TAppFjxxb> fjxxqb = fjxxDao.queryBuilder();
							fjxxqb.where(TAppFjxxbDao.Properties.Wybs.eq(wybs));
						  DeleteQuery<TAppFjxxb> deletefjxx = fjxxqb.buildDelete();
							deletefjxx.executeDeleteWithoutDetachingEntities();
							fjxxDao.insertOrReplaceInTx(mDzCpkhXxDjEtc.getFjxxs());*/
						}catch(Exception e){
							ex = e;
							re = false;
						}
						List<DzCpkhXxDjEtc> mList = new ArrayList<DzCpkhXxDjEtc>();
						mList.add(mDzCpkhXxDjEtc);
						if (re){
							arg0.onNext(mList);
							arg0.onCompleted();
						} else{
							arg0.onError(ex);
						}
					}
				});
			}
		});
	}
  
	public Observable<List<DzCpkhXxDjEtc>> getAllDzcpkhxxetc(final String val){
		return Observable.create(new Observable.OnSubscribe<List<DzCpkhXxDjEtc>>() {

			@Override
			public void call(Subscriber<? super List<DzCpkhXxDjEtc>> arg0) {
				// TODO Auto-generated method stub
				DzCpkhXxDjEtcDao dao = BankApplication.getDaoSession().getDzCpkhXxDjEtcDao();
				QueryBuilder<DzCpkhXxDjEtc> qb = dao.queryBuilder();
				if (val != null && !val.equals(""))
					if(StringUtils.isNumeric(val)==true){
						qb.where(DzCpkhXxDjEtcDao.Properties.KhZjh.like("%"+val+"%"));
					}else{
						qb.where(DzCpkhXxDjEtcDao.Properties.KhMc.like("%"+val+"%"));
					}
				
				qb.where(DzCpkhXxDjEtcDao.Properties.Xgzt.eq("0"));
				  qb.orderDesc(DzCpkhXxDjEtcDao.Properties.SqSj);
				
				List<DzCpkhXxDjEtc> mList = qb.list();
				if (mList == null){
					arg0.onError(null);
				} else {
					arg0.onNext(mList);
					arg0.onCompleted();
				}
			 
			}
		});
	}
	public Observable<DzCpkhXxDjEtc> getDzcpkhxxetc(final String uuid){
		return Observable.create(new Observable.OnSubscribe<DzCpkhXxDjEtc>() {

			@Override
			public void call(Subscriber<? super DzCpkhXxDjEtc> arg0) {
				// TODO Auto-generated method stub
				DzCpkhXxDjEtcDao dao = BankApplication.getDaoSession().getDzCpkhXxDjEtcDao();
				QueryBuilder<DzCpkhXxDjEtc> qb = dao.queryBuilder();
				if (uuid != null && !uuid.equals(""))
					qb.where(DzCpkhXxDjEtcDao.Properties.AppUuid.eq(uuid));
				qb.orderAsc(DzCpkhXxDjEtcDao.Properties.Id);
				
				TAppFjxxbDao fjDao= BankApplication.getDaoSession().getTAppFjxxbDao();
				QueryBuilder<TAppFjxxb> fjqb=fjDao.queryBuilder();
				fjqb.where(TAppFjxxbDao.Properties.Wybs.eq(uuid));
				List<TAppFjxxb> fjList = fjqb.list();
				
				List<DzCpkhXxDjEtc> mList = qb.list();
				DzCpkhXxDjEtc mDzCpkhXxDjEtc =mList.get(0);
				if(fjList.size()>0) {
//					mDzCpkhXxDjEtc.getFjxxs().addAll(fjList);
					mDzCpkhXxDjEtc.setFjxxs(fjList);
				}

				if (mList == null){
					arg0.onError(null);
				} else {
					
					arg0.onNext(mDzCpkhXxDjEtc);
					arg0.onCompleted();
				}
				
				
			}	
		});
	}
	public Observable<List<DzCpkhXxDjEtc>> uploadDzcpkhxxetc(final String uuid){
		return Observable.create(new Observable.OnSubscribe<List<DzCpkhXxDjEtc>>() {

			@Override
			public void call(Subscriber<? super List<DzCpkhXxDjEtc>> arg0) {
				// TODO Auto-generated method stub
				DzCpkhXxDjEtcDao dao = BankApplication.getDaoSession().getDzCpkhXxDjEtcDao();
				QueryBuilder<DzCpkhXxDjEtc> qb = dao.queryBuilder();
				if (uuid != null && !uuid.equals(""))
					qb.where(DzCpkhXxDjEtcDao.Properties.AppUuid.eq(uuid));
				qb.orderAsc(DzCpkhXxDjEtcDao.Properties.Id);
				
				TAppFjxxbDao fjDao= BankApplication.getDaoSession().getTAppFjxxbDao();
				QueryBuilder<TAppFjxxb> fjqb=fjDao.queryBuilder();
				fjqb.where(TAppFjxxbDao.Properties.Wybs.eq(uuid));
				List<TAppFjxxb> fjList = fjqb.list();
				
				List<DzCpkhXxDjEtc> mList = qb.list();
				DzCpkhXxDjEtc mDzCpkhXxDjEtc = new DzCpkhXxDjEtc();
				if(mList.size()==1){
				 mDzCpkhXxDjEtc =mList.get(0);}
				if(fjList.size()>0){
				mDzCpkhXxDjEtc.setFjxxs(fjList);
//				mDzCpkhXxDjEtc.getFjxxs().addAll(fjList);
				}
				if (mList == null){
					arg0.onError(null);
				} else {
					
					arg0.onNext(mList);
					arg0.onCompleted();
				}
				
				
			}	
		});
	}
	
	public Observable<List<DzCpkhXxDjEtc>> deleteDzcpkhxxetc(final String uuid){
		return Observable.create(new Observable.OnSubscribe<List<DzCpkhXxDjEtc>>() {

			@Override
			public void call(Subscriber<? super List<DzCpkhXxDjEtc>> arg0) {
				// TODO Auto-generated method stub
				DzCpkhXxDjEtcDao dao = BankApplication.getDaoSession().getDzCpkhXxDjEtcDao();
				QueryBuilder<DzCpkhXxDjEtc> qb = dao.queryBuilder();
				if (uuid != null && !uuid.equals(""))
					qb.where(DzCpkhXxDjEtcDao.Properties.AppUuid.eq(uuid));
				
				List<DzCpkhXxDjEtc> mList = qb.list();
				if(mList.size()==1){
					dao.delete(mList.get(0));
				}
			 
			}
		});
}
	public Observable<String> getAllBm(String  content){
		return HttpMethodServlet.getInstance().getAllBm(content);
	}
	public Observable<List<DzCpkhXxDjEtc>> getAllDzcpkhxxetcList(final int offset , final String time){
		return Observable.create(new Observable.OnSubscribe<List<DzCpkhXxDjEtc>>() {

			@Override
			public void call(Subscriber<? super List<DzCpkhXxDjEtc>> arg0) {
				// TODO Auto-generated method stub
				DzCpkhXxDjEtcDao dao = BankApplication.getDaoSession().getDzCpkhXxDjEtcDao();
				/*QueryBuilder<DzCpkhXxDjEtc> qb = dao.queryBuilder();
				qb.orderDesc(Properties.Sqsj);*/
				QueryBuilder<DzCpkhXxDjEtc> qb = dao.queryBuilder().orderDesc(DzCpkhXxDjEtcDao.Properties.SqSj);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				
				Date date = null;
				if (offset == 0){
					date = new Date();
				} else {
					try {
						date=sdf.parse(time);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
//				if(time.equals("")){
				try {
					qb.where(DzCpkhXxDjEtcDao.Properties.SqSj.between(sdf.parse("2016-07-29 00:00:00"),date));
					List<DzCpkhXxDjEtc> mList = qb.list();
					qb.offset(0 * 10).limit(10);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
//				}else{
//					try {
//						
//						qb.where(Properties.Sqsj.between(sdf.parse("2016-07-29 00:00:00"),sdf.parse(time)));
//						List<DzCpkhXxDjEtc> mList = qb.list();
//						qb.offset(offset * 3).limit(3);
//					} catch (ParseException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//				}
				List<DzCpkhXxDjEtc> mList = qb.list();
				if (mList == null){
					arg0.onError(null);
				} else {
					arg0.onNext(mList);
					arg0.onCompleted();
				}
			 
			}
		});
	}
	
	public Observable<List<DzCpkhXxDjEtc>> getAllDzcpkhxxetcUpList(final int offset , final String time){
		return Observable.create(new Observable.OnSubscribe<List<DzCpkhXxDjEtc>>() {

			@Override
			public void call(Subscriber<? super List<DzCpkhXxDjEtc>> arg0) {
				// TODO Auto-generated method stub
				DzCpkhXxDjEtcDao dao = BankApplication.getDaoSession().getDzCpkhXxDjEtcDao();
				/*QueryBuilder<DzCpkhXxDjEtc> qb = dao.queryBuilder();
				qb.orderDesc(Properties.Sqsj);*/
				QueryBuilder<DzCpkhXxDjEtc> qb = dao.queryBuilder().orderDesc(DzCpkhXxDjEtcDao.Properties.SqSj);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date date = null;
				
				try {
					date = sdf.parse(time);
					qb.where(DzCpkhXxDjEtcDao.Properties.SqSj.between(time,new Date()));
					List<DzCpkhXxDjEtc> mList = qb.list();
					qb.offset(offset * 10).limit(10);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				List<DzCpkhXxDjEtc> mList = qb.list();
				if (mList == null){
					arg0.onError(null);
				} else {
					arg0.onNext(mList);
					arg0.onCompleted();
				}
			 
			}
		});
	}
	
	public Observable<String> updateDzcpkhxxetc(String content){
		return HttpMethodServlet.getInstance().upLoadDzcpEtc(content);
	}
	
	/**
	 * 更新状态
	 * @param arg0
	 */
	public void UpdateZtByDzpEtcBySql(String arg0){
		List<SynchronizeBean> beans = GsonUtil.getSynchronizeBean(arg0);
		DaoSession session = BankApplication.getDaoSession();
		for (SynchronizeBean bean : beans){
			String sql = "update DZ_CPKH_XX_DJ_ETC set xgzt = 1 where app_Uuid = '"+bean.getWybs()+"'";
			session.getDatabase().execSQL(sql);
		}
	}
	

	
	//etc列表未成交上拉显示
	public Observable<String> getDzcpEtcListData(String content){
		return HttpMethodServlet.getInstance().getDzcpEtcListData(content);
	}

	//  时间: 2020/4/21 4:46 PM ,作者: xjc, 作用:ETC删除
	public Observable<String> getDzcpEtcDelData(String content){
		return HttpMethodServlet.getInstance().getDzcpEtcDelData(content);
	}

	//etc列表未成交下拉显示
		public Observable<String> getDzcpEtcUpListData(String content){
			return HttpMethodServlet.getInstance().getDzcpEtcUpListData(content);
		}
	//etc列表查询查看显示
	public Observable<String> serachXx(String content){
			return HttpMethodServlet.getInstance().serachXx(content);
		}
	//etc列表已成交上拉显示
	public Observable<String> getDzcpEtcYcjListData(String content){
			return HttpMethodServlet.getInstance().getDzcpEtcYcjListData(content);
		}
	//etc列表已成交下拉刷新显示
	public Observable<String> getDzcpEtcYcjUpListData(String content){
				return HttpMethodServlet.getInstance().getDzcpEtcYcjUpListData(content);
			}
		
		//etc列表未成交查询查看显示
	public Observable<String> getBdserach(String content){
				return HttpMethodServlet.getInstance().getBdserach(content);
			}
		//etc列表未成交查询查看显示getBdYcjserach
	public Observable<String> getBdYcjserach(String content){
				return HttpMethodServlet.getInstance().getBdYcjserach(content);
					}
	
	//pos列表未成交上拉显示
		public Observable<String> getWcjPosListData(String content){
			return HttpMethodServlet.getInstance().getWcjPosListData(content);
		}
}
